<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Caigou\Controller;
use Think\Controller;
use User\Api\UserApi;
use Think\Model;

class IndexController extends Controller {
    /**
     * 采购首页
     */
    public function index(){
        $this->title="采购列表";
        $this->display();
    }
    /**
     * 商品详情
     */
    public function detail($id){
        $this->id=$id;
        $this->display();
    }
    /**
     * 添加采购商品
     */
    public function newproduct(){
        if(IS_POST){
            if(token_is_lock()){
                $this->redirect('Index/index');
                return;
            }
            $model=M('caigou_goods');
            $model->create();
            $model->createAt=time();
            $model->updateAt=time();
            if(I('id')!=""){
                $info=$model->where(array('id'=>I('id')))->find();
                if($info['status']<=2){
                    $res=$model->save();
                }else{
                    $this->error('后台已处理，请联系财务人工修改',U('Index/history'));
                }
            }else{
                $res=$model->add();
            }
            token_lock();
            if($res){
                $this->redirect('Index/index');
            }else{
                $this->error("商品添加失败");
            }
        }else{
            token_unlock();
            $batch = I("batch");
            $this->batch = $batch;
            $this->display();
        }
    }
    /**
     * 原包装库
     */
    public function inventoryybz($showAll=0){

        $map['BIN ']=array('like',"1%");
        $list = M('vinventory')->where($map)->select();
        $hideArr=M('inventory_hide')->getField('BIN',true);
        $this->assign('_list', $list);
        $this->assign('hideArr', $hideArr);
        $this->title = '原包装库';
        $this->showAll = $showAll;
        $this->display('Index/inventory');
    }
    /**
     *成品库
     */
    public function inventorycp(){
        $map['BIN ']=array('like',"2%");
        $list = M('vinventory')->where($map)->select();
        int_to_string($list);
        $this->assign('_list', $list);
        $this->title = '成品库';
        $this->display('Index/inventory');
    }
    /**
     * 获取历史批次
     */
    public function history(){
        $sql="select batch,count(id) cnt,status from gr_caigou_goods where status>1 group by batch order by batch desc ";
        $model=new Model();
        $res=$model->query($sql);
        $this->assign("_list",$res);
        $this->display();
    }
    /**
     * 隐藏部分商品
     * @param $BIN
     */
    public function hide($BIN){
        $model=M('inventory_hide');
        $data['BIN']=$BIN;
        $data['createAt']=time();
        $res=$model->add($data);
        if($res){
            $this->success('隐藏成功，你可以点击展示全部找到它');
        }else{
            $this->error('隐藏失败');
        }
    }
    public function show($BIN){
        $model=M('inventory_hide');
        $map['BIN']=$BIN;
        $res=$model->where($map)->delete();
        if($res){
            $this->success('显示成功');
        }else{
            $this->error('显示失败');
        }
    }
    /**
     * 历史明细
     * @param $batch
     *
     */
    public function historydetail($batch){
        $this->batch=$batch;
        $this->display();
    }
    /**
     * ajax获取商品列表
     */
    public function ajaxgetgoods($batch=""){
        $map['status']=1;
        if($batch!=""){
            $map['batch']=$batch;
            $map['status']=array('gt',1);
        }
        $model=M('caigou_goods');
        $map['delflag']=0;
        $res=$model->where($map)->select();
        $this->ajaxReturn($res);
    }
    /**
     * ajax删除商品
     */
    public function ajaxdelgoods($id=0){
        $model=M('caigou_goods');
        $map['id']=$id;
        $data['delflag']=1;
        $res=$model->where($map)->save($data);
        $this->ajaxReturn($res);
    }
    /**
     * ajax删除商品
     */
    public function ajaxsubmitgoods(){
        $model=M('caigou_goods');
        $map['status']=1;
        $data['status']=2;
        $data['batch']=date("Y-m-d",time());
        $data['updateAt']=time();
        $res=$model->where($map)->save($data);
        $this->ajaxReturn($res);
    }
    /**
     * ajax获取商品详情
     */
    public function ajaxgetgood($id){
        $map['id']=$id;
        $res=M("Caigou_goods")->where($map)->find();
        $this->ajaxReturn($res);
    }
}
